class AjaxController < ApplicationController
    def get_agent
        @response.headers["Content-Type"] = "text/xml; charset=utf-8" 
        if request.get?
            conditions=''
            if params[:agent_name]
                conditions += "name like '%#{params[:agent_name]}%'"
            end
            if params[:agent_part]
                if !conditions.eql? ''
                    conditions += ' and '
                end
                conditions += "part like '%#{params[:agent_part]}%'"
            end
            if !conditions.eql? ''
                if !@session[:account].role.memo.include? 'salemanager'
                    conditions += " and employee_id=#{@session[:account].employee_id}"
                end
            @agents = Agent.find(:all,:conditions=>conditions)
            else
                if !@session[:account].role.memo.eql? 'salemanager'
                    @agents = Agent.find(:all,:conditions=>"employee_id=#{@session[:account].employee_id}")
                else
                    @agents = Agent.find_all
                end

            end
        end
    end

    def get_product
        name = params[:product_name]
        serial = params[:product_serial]
        if name.eql? ''
            name = nil
        end
        if serial.eql? ''
            serial = nil
        end
        condition = ''
        if name && serial
            condition = "name like '%#{name}%' and serial like '%#{serial}%'"
        elsif serial && !name
            condition = "serial like '%#{serial}%'"
        elsif !serial && name
            condition = "name like '%#{name}%'"
        end 
        if !condition.eql? ''
            condition += ' and state=1'
            @products = Product.find(:all,:conditions=>condition)
        elsif
            @products = Product.find(:all,:conditions=>'state=1')
        end
        #取默认出货仓库
        defStore = Store.find(:first,:conditions=>"out_type=1")
        @storeCount = Hash.new
        @products.each do |product|
            sp = StoreProduct.find(:first,:conditions=>"product_id=#{product.id} and store_id=#{defStore.id}")
            puts "product_id=#{product.id} and store_id=#{defStore.id}"
            spCount = 0
            spCount = sp.current_count if sp
            puts product.id
            puts spCount
            @storeCount[product.id] = spCount
        end
        @response.headers["Content-Type"] = "text/xml; charset=utf-8" 

    end

    def get_lastserial
        #debugger
        # condition = "papertype='GO' or papertype='NDFD'"
        #上一次填写的送货单号/日期
        if @session[:item_id]
            @lastserial =  SalePaperItem.find(:first,:conditions=>"id = #{@session[:item_id]}")
        else
            @lastserial = SalePaperItem.find(:first,:order=>'id desc')
        end
        @response.headers["Content-Type"] = "text/plain; charset=utf-8" 
    end

    def get_lastdate
        #debugger
        # condition = "papertype='GO' or papertype='NDFD'"
        #上一次填写的送货单号/日期
        if @session[:item_id]
            @lastdate =  SalePaperItem.find(:first,:conditions=>"id = #{@session[:item_id]}")
        else
            @lastdate = SalePaperItem.find(:first,:order=>'id desc')
        end
        @response.headers["Content-Type"] = "text/plain; charset=utf-8" 
    end

end
